package com.wjm.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjm.entity.Desk;
import com.wjm.service.impl.ClassroomServiceImpl;
import com.wjm.service.impl.DeskServiceImpl;
import com.wjm.util.CacheUtil;
import com.wjm.util.JsonUtil;

@Controller
@RequestMapping("/web/desk")
public class DeskWebController {

	@Resource(name = "deskService")
	private DeskServiceImpl deskService;

	@Resource(name = "classroomService")
	private ClassroomServiceImpl classroomService;

	@RequestMapping("/query")
	@ResponseBody
	public String query(int crId, int length) {
		String result = "";
		try {
			List<Desk> list = CacheUtil.getDesks().get(crId);
			int[] crIds = null;
			if (list != null && list.size() > 0) {
				crIds = new int[list.size()];
				for (int i = 0; i < list.size(); i++) {
					Desk desk = list.get(i);
					// crIds[i] = (desk.getDeskPosR() - 1) * length +
					// desk.getDeskPosC();
					crIds[i] = desk.getDeskNo();
				}
				System.out.println(crIds.length);
				result = "{\"result\":\"success\",\"deskNo\":\""
						+ JsonUtil.getJson(crIds) + "\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/add")
	@ResponseBody
	public String add(String pos, int length, int crId) {
		String result = "";
		try {
			String[] res = pos.trim().split(",");
			List<Desk> desks = new ArrayList<>();
			Desk desk = null;
			List<Desk> list = CacheUtil.getDesks().get(crId);
			if (list != null && list.size() > 0) {
				int buId = classroomService.queryBelong(crId);
				// 检查传过来的座位号，是否有删除
				go: for (int i = 0; i < list.size(); i++) {
					for (int j = 0; j < res.length; j++) {
						if (!res[j].equals("0")) {
							int num = Integer.parseInt(res[j]);
							if (num == list.get(i).getDeskNo()) {
								res[j] = "0";
								continue go;
							}
						}
					}
					deskService.delete(list.get(i).getDeskId());
					list.remove(i);
					i--;
					// 每次删除座位都需要进行不可用检查
					classroomService.checkUnuse(crId, buId);
				}
			}
			for (String str : res) {
				if (!str.equals("0")) {
					int num = Integer.parseInt(str);
					int r = num % length == 0 ? (num / length)
							: (num / length + 1);
					int c = num % length;
					desk = new Desk();
					desk.setCrId(crId);
					desk.setDeskNo(num);
					desk.setDeskPosC(c);
					desk.setDeskPosR(r);
					desks.add(desk);
				}
			}

			// 批量添加
			int nums = 0;
			if (desks != null && desks.size() > 0) {
				nums = deskService.saveBatch(desks);
			}
			// 如果有新座位添加那该教室必定是可用的
			if (nums > 0) {
				classroomService.checkUse(crId);
			}
			// 将添加的查询出来（有批量添加没有办法获取主键）
			desks = deskService.queryByCr(crId);
			CacheUtil.getDesks().put(crId, desks);
			result = "{\"result\":\"success\",\"nums\":\"" + nums + "\"}";
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

}
